package com.zenithmind.coding.controller;

import com.zenithmind.common.result.Result;
import com.zenithmind.coding.controller.base.BaseController;
import com.zenithmind.coding.pojo.vo.ProblemVO;
import com.zenithmind.coding.service.ProblemRecommendationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 问题推荐控制器 - 遵循单一职责原则，专门处理问题推荐相关功能
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Tag(name = "问题推荐", description = "编程问题推荐相关接口")
@RestController
@RequestMapping("/api/zenithMind/v1/problems/recommendation")
@RequiredArgsConstructor
public class ProblemRecommendationController extends BaseController {

    private final ProblemRecommendationService problemRecommendationService;

    @Operation(summary = "查询推荐问题", description = "查询个性化推荐问题")
    @GetMapping
    public Result<List<ProblemVO>> getRecommendProblems(
            @Parameter(description = "难度等级") @RequestParam(required = false) Integer difficulty,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "10") Integer limit) {
        String userId = getCurrentUserId();
        List<ProblemVO> problems = problemRecommendationService.getRecommendProblems(userId, difficulty, limit);
        logOperation("查询推荐问题", "难度: " + difficulty + ", 数量: " + limit);
        return success(problems);
    }

    @Operation(summary = "查询热门问题", description = "查询热门编程问题")
    @GetMapping("/hot")
    public Result<List<ProblemVO>> getHotProblems(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "10") Integer limit) {
        List<ProblemVO> problems = problemRecommendationService.getHotProblems(limit);
        return success(problems);
    }

    @Operation(summary = "查询相关问题", description = "查询相关编程问题")
    @GetMapping("/{id}/related")
    public Result<List<ProblemVO>> getRelatedProblems(
            @Parameter(description = "问题ID") @PathVariable String id,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "5") Integer limit) {
        validateId(id, "问题ID");
        List<ProblemVO> problems = problemRecommendationService.getRelatedProblems(id, limit);
        return success(problems);
    }

    @Operation(summary = "根据标签查询问题", description = "根据标签查询编程问题")
    @GetMapping("/tags")
    public Result<List<ProblemVO>> getProblemsByTags(
            @Parameter(description = "标签列表") @RequestParam List<String> tags,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "10") Integer limit) {
        List<ProblemVO> problems = problemRecommendationService.getProblemsByTags(tags, limit);
        return success(problems);
    }

    @Operation(summary = "搜索问题", description = "根据关键词搜索编程问题")
    @GetMapping("/search")
    public Result<List<ProblemVO>> searchProblems(
            @Parameter(description = "关键词") @RequestParam String keyword,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "20") Integer limit) {
        List<ProblemVO> problems = problemRecommendationService.searchProblems(keyword, limit);
        logOperation("搜索问题", "关键词: " + keyword);
        return success(problems);
    }

    @Operation(summary = "获取每日挑战", description = "获取每日编程挑战问题")
    @GetMapping("/daily-challenges")
    public Result<List<ProblemVO>> getDailyChallenges(
            @Parameter(description = "数量") @RequestParam(defaultValue = "3") Integer count) {
        String userId = getCurrentUserId();
        List<ProblemVO> problems = problemRecommendationService.getDailyChallenges(userId, count);
        return success(problems);
    }

    @Operation(summary = "获取个性化学习路径", description = "获取基于用户能力的学习路径推荐")
    @GetMapping("/learning-path")
    public Result<List<ProblemVO>> getLearningPath(
            @Parameter(description = "路径长度") @RequestParam(defaultValue = "10") Integer pathLength) {
        String userId = getCurrentUserId();
        List<ProblemVO> problems = problemRecommendationService.getLearningPath(userId, pathLength);
        return success(problems);
    }

    @Operation(summary = "获取弱项强化问题", description = "获取针对用户弱项的强化练习问题")
    @GetMapping("/weakness-reinforcement")
    public Result<List<ProblemVO>> getWeaknessReinforcementProblems(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "5") Integer limit) {
        String userId = getCurrentUserId();
        List<ProblemVO> problems = problemRecommendationService.getWeaknessReinforcementProblems(userId, limit);
        return success(problems);
    }

    @Operation(summary = "获取同级别推荐", description = "获取与用户当前水平匹配的问题推荐")
    @GetMapping("/level-matched")
    public Result<List<ProblemVO>> getLevelMatchedProblems(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "10") Integer limit) {
        String userId = getCurrentUserId();
        List<ProblemVO> problems = problemRecommendationService.getLevelMatchedProblems(userId, limit);
        return success(problems);
    }

    @Operation(summary = "获取进阶挑战", description = "获取比用户当前水平稍高的挑战问题")
    @GetMapping("/advanced-challenges")
    public Result<List<ProblemVO>> getAdvancedChallenges(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "5") Integer limit) {
        String userId = getCurrentUserId();
        List<ProblemVO> problems = problemRecommendationService.getAdvancedChallenges(userId, limit);
        return success(problems);
    }
}
